/*
 * 诊断组件抽取(诊断的div需要自己写)
*/
//根据参数初始化诊断框
function initDiagnoseByParam(input){
	if (valueArr["p_9999_9999_115"]!="2"){  //不等于2 为有自定义诊断，可以编辑
		$(input).tagsinput({allowKeypressAdd:true });
	}else{
		$(input).tagsinput({allowKeypressAdd:false });
	}
}
function initDiagnoseByParam_single(input){
	if (valueArr["p_9999_9999_115"]!="2"){
		$(input).tagsinput({allowKeypressAdd:true,onlyOne:true});
	}else{
		$(input).tagsinput({allowKeypressAdd:false,onlyOne:true});
	}
}
//根据参数更改诊断显示
function loadTplByParam(){
	//诊断录入选项参数
	var obj={};
	var key = valueArr["p_9999_9999_115"];
	switch(key)
	{
	case '1':
		//ICD诊断和自定义诊断
		obj.selfwrite = true;
		obj.ICD = true;
	  break;
	case '2':
		//ICD诊断
		obj.selfwrite = true;
		obj.ICD = true;
	  break;
	case '3':
		// 自定义诊断
		obj.selfwrite = true;
		obj.ICD = false;
	}
	//医保或非医保诊断参数
	obj.isBdp02 = false;
	 if( $("#patient_bdp02").text()!="自费" && valueArr["p_100_105002_271"]=="1"){ //医保
		 obj.isBdp02 = true;
	   }
	return obj;
}
//新开界面诊断下拉功能
function diagonoseNewOrder(){
	debugger;
	var showObj = loadTplByParam();
  //新开页面加载诊断智能搜索输入框
	if(showObj.ICD){
		$("#tagsinput_item .tagsinput_event").tooltip({id:"diagnose",showObj:showObj});  //智能搜索
	}
	$("#tagsinput_item .tagsinput_event").dropbox({   //诊断下拉框
			id:"new_diagnose",
			tpl:"diagnose.html",
			showObj:showObj,
			css:{minHeight:"600px"},
			event:function(_option){
				selfDiagnoseEvent('show'); //自定义诊断tab1
				callBackOrderDisgnose(_option); //ICD10诊断 tab2
				addhistroyDiagnose(_option);//加载历史诊断tab3
			}
		});

}
//加载ICD10诊断,区分医保和非医保
function  callBackOrderDisgnose(_option){
	if( $("#patient_bdp02").text()!="自费" && valueArr["p_100_105002_271"]=="1"){ //医保
		MedicalInsureDiagnose(_option);
		
	}else{ //自费
		addDiseaseData(_option);
	}
}

/*----------加载树-------------*/
function addDiseaseData(_option){
	var $tree_div = $('#'+_option.dropid).find(".drop-left");
	_option.$table_div = $('#'+_option.dropid).find(".drop-right .table");
	var flag_url = getUrlParam("yiyuanId");
	var dtemp;
	if(flag_url){
		dtemp = jibingList;
	}else{
		dtemp = parent.jibingList;
	}
	$tree_div.treeview({data: dtemp});
	$tree_div.on('nodeSelected', function(event, data) {
		// 查询疾病详细项目列表
		_option.bah01 = data['id'];
		treeClickList(_option);
	});
}
//树点击事件
function treeClickList(_option){
	debugger;
	var url = rootUrl+"/treatment/jibingInfo";
	var params = {};
	params.bah01 = _option.bah01;
	$.get(url,params,function(data){
		if(data){
			laytplUrl(myurl+"/module/tree-table-jibing.html",data,function(data,html){
				_option.$table_div.empty().append(html);
				_option.$table_div.find("tbody tr:eq(0)").addClass("choseedTr");
				var obj={};
				obj.$table = _option.$table_div;
				obj.$target = _option.$target;
				selectSelfDiagnose(obj); //表格和target
			});
		}
	});
}
//历史诊断
function addhistroyDiagnose(_option){
	 var url =  rootUrl+"/clidoctor/selectDataByScriptId";
	 var params8 = {},para={};
		params8.vaa01 = vaa01;
		params8.acf01 = 1;
		params8.vaa07 = vac01;
		params8.currPage = 1;
		params8.pageSize = 10;
		para.paramStr = JSON.stringify(params8);
		para.ID = "10000040";
	    $.when(
	        $.get(url,para)  //历史诊断
	    ).done(function(data){
	    	debugger;
	    	var $history_div = $('#'+_option.dropid).find("#history-diagnose .main-table-container ");
	    	laytplUrl(myurl + "/module/zhenduan_table.html", data.list, function(data, html)
	    			{
			    		$history_div.empty().append(html);
			    		var obj={};
			    		obj.$table = $history_div.find("table");
			    		obj.$target = _option.$target;
			    		selectSelfDiagnose(obj); //表格和target
	    			});
	    	
	    })
}

//医保诊断
//加载医保数据
function MedicalInsureDiagnose(_option){
	var $drop_right = $('#'+_option.dropid).find(".drop-right");
	var $table_div = $('#'+_option.dropid).find(".drop-right .table");
	$drop_right.css({"left":"7px"});
	// yibaoJibingList 页面初始加载的数据
	var flag_url = getUrlParam("yiyuanId");
	var dtemp;
	if(flag_url){
		dtemp = yibaoJibingList.slice(1, 20);
	}else{
		dtemp = parent.yibaoJibingList.slice(1, 20);
	}
	var data = {
			list :dtemp,
			type : "yibao"
	};
	laytplUrl(myurl+"/module/tree-table-jibing-search.html",data,function(data,html){
		$table_div.empty().append(html);
		$table_div.find("table tbody tr:eq(0)").addClass("choseedTr");
	});
	
}

//诊断添加数据---
//医保诊断
function addMedicaDiagnose(that){
	debugger;
	var str = $(that).attr("data-info");
	var zdname  = JSON.parse(str).iad04;
	if($(".shouye_zdms").length>0){
		$("#"+shouyeActiveId).val(zdname);
	}else{
		$("#diagnose_tip").tagsinput('add', zdname,str);
	}
}

// 非医保诊断
//诊断标签添加1 cam01类型
function zdSelectResult(zdid, zdname)
{
	if ($("#diagnose_tip").length>0)
	{
		var str = '{"bak01":"'+zdid+'"}';
		$("#diagnose_tip").tagsinput('add', zdname,str);
	}
	else
	{
		add_zhenduan(zdid, zdname);
	}
}

//编辑诊断添加
function add_zhenduan(zdid, zdname)
{
	$("#" + shouyeActiveId).val(zdname);
	$("#" + shouyeActiveId).attr("data-info", zdid);
	$("#index_edit_diagnose  .drop_close_btn").click();//关闭编辑诊断框
}
/*---------------------------------------------------------诊断添加数据-------------------------------------------------------------------*/
//编辑诊断添加 
function editZhenDuanEvent(cbm01){
	debugger;
	var lVAA01 = $("#vaa01").val();
	var lVAE01 = $("#vac01").val();
    var lVAF01 = cbm01;
    var lVAF01A =cbm01;
    var acf01 = 1;
	$.ajax({
		url : rootUrl + "/doctoradviceeditor/storagenewdiagnosisrecord",
		type : "post",
		dataType : 'json',
		data:{"lVAA01":lVAA01,"lVAE01":lVAE01,"lVAF01":lVAF01,"lVAF01A":lVAF01A,"acf01":acf01},
		success : function(data) {
			if(data){
				var list = data.list;
				for(var i=0;i<list.length;i++){
					var str = JSON.stringify(list[i]);
					$("#diagnose_tip").tagsinput('add',list[i]['bak05'],str);//name id 替换为数据里的就好了
				}
			}
		}
	});
}

//新开加载所有诊断
function addZhenDuanEvent(cbm01){
	debugger;
	var lVAA01 = $("#vaa01").val();
	var lVAE01 = $("#vac01").val();
	var lVAF01 = cbm01;
	var lVAF01A =cbm01;
	var acf01 = 1;
	$.ajax({
		url : rootUrl + "/doctoradviceeditor/storagenewdiagnosisrecord1",
		type : "post",
		dataType : 'json',
		data:{"lVAA01":lVAA01,"lVAE01":lVAE01,"lVAF01":lVAF01,"lVAF01A":lVAF01A,"acf01":acf01},
		success : function(data) {
			debugger;
			if(data){
				var list = data.list;
				for(var i=0;i<list.length;i++){
					var str = JSON.stringify(list[i]);
					$("#diagnose_tip").tagsinput('add',list[i]['bak05'],str);//name id 替换为数据里的就好了
				}
			}
		}
	});
}
/*-----------------------------------------------------------------自定义诊断-------------------------------------------------------------------------------------------------*/
function selfDiagnoseEvent(tpltype)
{
	var showObj = loadTplByParam();
	var url =  rootUrl+"/clidoctor/selectDataByScriptId";
	var text = $("#search_jibing").val()?$("#search_jibing").val():"";
	var para =
	{
	bce01 : bce01,
	text : "%" + text + "%"
	}
	var param =
	{
		ID :"10000039",
		paramStr : JSON.stringify(para)
	};
	$.when($.get(url, param)).done(function(data)
	{
		var list = data.list;
		var obj =
		{};
		obj.list = list;
		obj.type = tpltype;
		laytplUrl_T(myurl + "/module/self-diagnose.html", obj, function(data, html)
		{
			$("#self-diagnose").empty().append(html);
			initDiagnoseByParam_single($(".add_self_d"));
			disableForFirst();
			if (tpltype == "manage")
			{
				debugger;
				if(showObj.ICD){
					showObj.isBdp02 = false;
					$("#self-diagnose").find(".tagsinput_event").tooltip({id:"diagnose",showObj:showObj});
				}
				for (var i = 0; i < list.length; i++)
				{
					var str1 = '{"bak01":"' + list[i].bak01 + '","bak02":"' + list[i].bak02 + '"}';
					$("#self-diagnose").find(".add_self_d:eq(" + i + ")").tagsinput('add', list[i].bak05, str1);
					$("#self-diagnose").find(".self-code:eq(" + i + ")").val(list[i].bak02);
				}
			}else{
				//注册选中事件
				var obj={};
				obj.$table = $("#self-diagnose-table");
				obj.$target = $(".tagsinput_event");
				selectSelfDiagnose(obj);
			}
		})
	})
}

function selectSelfDiagnose(obj){
	debugger;
		 obj.$table.find("tbody tr").off("dblclick").on("dblclick",function(){
		 obj.$target.val("");   //清空内容
		 var bak01 = $(this).attr('data-bak01');
		 var bak05 = $(this).find(".name_td").text().trim();
		 var str = '{"bak01":"'+bak01+'"}';
		 var _target = $(obj.$target).parents(".bootstrap-tagsinput").next("input[type='text']")
		 if(_target.length>0){
			 _target.tagsinput('add', bak05, str);
		 }else{
			 obj.$target.val(bak05);
			 obj.$target.attr("data-info",bak01);
		 }
	})
}
//自定义诊断管理
function manageChangeEvent(){
	selfDiagnoseEvent("manage");
}

//自定义诊断保存
function saveSelfDiagnose(){
	var vaoList = [];
	$("#self-diagnose-table tbody tr").each(function(i,e){
		var obj={};
		obj.bce01= bce01;
		var diagnose=  $(e).find(".label-info");
		if(diagnose.length >0){
			var diaostr = diagnose.attr("data-info");
			var diaobj = JSON.parse(diaostr)||'-999';
			if(diaobj == -999){
				obj.bak01 =  diaobj; //id
				obj.bak02 =  $(e).find(".self-code").val(); //编码
				
			}else if(diaobj.bak01 == -999){
				obj.bak01 =  diaobj.bak01; //id
				obj.bak02 =  $(e).find(".self-code").val(); //编码
			}
			else{
				
				obj.bak01 =  diaobj.bak01; //id
				obj.bak02 =  diaobj.bak02; //id
			}
			obj.bak05 =  diagnose.text().trim(); //名字
			obj.rownr= $(e).find(".self-num").val();
			if(obj.bak05){
				vaoList.push(obj);
			}
		}
		
	})
	var url = rootUrl+ "/clidoctor/updateBce01_vao1";
	var para = {
				bce01:bce01,
				vaoList:vaoList
				}
	var param = {
			      params:JSON.stringify(para)
				 };
	$.when($.post(url, param)).done(function(data) {
		selfDiagnoseEvent("show");
		
	})
}

//自定义诊断新增
function addSelfDiagnose(){
	var showObj = loadTplByParam();
	var maxnum=0;//获取最大行号
	$("#self-diagnose-table tbody tr").each(function(i,e){
		var num = $(e).find(".self-num").val();
		if(parseInt(maxnum) < parseInt(num)){
			maxnum = num;
		}
	})	
	var data = {};
	data.maxnum = parseInt(maxnum)+1;
	laytplUrl_T(myurl + "/module/self-diagnose-add.html", data, function(data, html)
		{
			$("#self-diagnose-table").prepend(html);
			initDiagnoseByParam_single($(".add_self_d"));
			var newtr = $("#self-diagnose-table").find("tbody tr:eq(0)");
			if(showObj.ICD){
				showObj.isBdp02 = false;
				$(newtr).find(".tagsinput_event").tooltip({id:"diagnose",showObj:showObj});
			}
			$(newtr).find(".tagsinput_event").focus();
			disableForFirst();
		})	
}
//自定义诊断取消
function cancelSelfDiagnose(){
	selfDiagnoseEvent("show");
}

//诊断上下移动
function upmove(that){
	var $cur_row = $(that).parents("tr");
	$cur_row.fadeOut(100,function(){
	    var $prev_row = $cur_row.prev();
	    $prev_row.before($cur_row);
	    $cur_row.fadeIn(100, function(){
	    	disableForFirst();
	    });
	});
	exchangeRownr(that,"before");
}
function downmove(that){
	var $cur_row = $(that).parents("tr");
	$cur_row.fadeOut(100,function(){
	    var $next_row = $cur_row.next();
	    $next_row.after($cur_row);
	    $cur_row.fadeIn(100, function(){
	    	disableForFirst();
	    });
	});
	exchangeRownr(that,"next");
	
}
function exchangeRownr(that,o){
	var $cur_row = $(that).parents("tr");
	var cur_tr = $cur_row.find(".self-num"); //当前诊断的rownr
	var cur_rownr = cur_tr.val();
	var other_tr;
	if(0 == "next"){
		 other_tr = $cur_row.next().find(".self-num");
	}else{
		
		 other_tr = $cur_row.prev().find(".self-num");
	}
	var other_rownr = other_tr.val();
	var temp = cur_rownr;
	cur_rownr.val(other_rownr);
	other_rownr.val(temp);
}
function deletmove(that){
	event.stopPropagation(); 
	$(that).parents("tr").remove();
	disableForFirst();
}

function disableForFirst(){
	$("#self-diagnose-table").find("button").removeAttr("disabled")
	$("#self-diagnose-table").find(".glyphicon-circle-arrow-up").first().parents("button").attr("disabled",true);
	$("#self-diagnose-table").find(".glyphicon-circle-arrow-down").last().parents("button").attr("disabled",true);
}

//自定义诊断添加数据
$('body').on('selected', '#diagnose_drop_content', function(e, data){
	debugger;
	  var $target = data.target;
      var selectedData = data.selectedData;
      var $_target_tr = $target.parents("tr");
      var str1 = '{"bak01":"'+ selectedData.bak01 +'","bak02":"'+selectedData.bak02+'"}';
      $_target_tr.find(".add_self_d").tagsinput('add', selectedData.bak05,str1);
      $_target_tr.find(".self-code").val(selectedData.bak02);
})

/*-----------------------------------------------------------------自定义诊断 end-------------------------------------------------------------------------------------------*/

//办理入院
function diagonoseInHospital(){
	var showObj = loadTplByParam();
	if(showObj.ICD){
		$("#tagsinput_item .tagsinput_event").tooltip({id:"diagnose",showObj:showObj,});  //智能搜索
	}
	$("#tagsinput_item .tagsinput_event").dropbox({
			id:"new_diagnose",
			tpl:"diagnose.html",
			showObj:showObj,
			css:{minHeight:"500px",zIndex:"99999999"},
			event:function(_option){
				selfDiagnoseEvent('show'); //自定义诊断tab1
				callBackOrderDisgnose(_option); //ICD10诊断 tab2,无历史诊断
			}
		});

}

//index 页面诊断编辑，（页面下面）//只能选，不能写
function  diagnoseInAdvice(){
	var showObj = loadTplByParam();
	$("#zhenduan_tbody .shouye_zdms ").dropbox({
		id:"new_diagnose",
		tpl:"diagnose.html",
		self:false,  //是否显示自定义诊断
		showObj:showObj,
		css:{minHeight:"500px",zIndex:"99999999",minWidth:'800px'},
		event:function(_option){
			callBackOrderDisgnose(_option); //ICD10诊断 tab2,无历史诊断
		}
	});
}



